function [ subjectinfo ] = analyzeVisualResponsivenessBanded_PublishResults( subjectinfo )
%ANALYZEVISUALRESPONSIVENESSBANDED_CALCULATE Summary of this function goes here
%   Detailed explanation goes here

% Clear output window.
clc;
if ~isa(subjectinfo, 'SubjectInfo')
    error('Invalid subjectInfo.');
end
taskname = 'analyzeVisualResponsivenessBanded_PublishResults';
% CheckTask(subjectinfo, taskname);
subjectinfo = DoWork(subjectinfo);
% CleanUp(subjectinfo, taskname);
end

%% Task related support
function CheckTask(subjectinfo, taskname)
% Check the task status
myTaskIndex = TaskInfo.FindTask(subjectinfo, taskname);
if (subjectinfo.Tasks(myTaskIndex).IsDone)
    disp(subjectinfo);
    disp(['Task "' taskname '" has already been completed.']);
    pause(5);
    return
end
if (subjectinfo.Tasks(myTaskIndex).HasDependancy)
    myDepTaskIndex = TaskInfo.FindTask(subjectinfo, subjectinfo.Tasks(myTaskIndex).Dependancy);
    if (subjectinfo.Tasks(myDepTaskIndex).IsToDo)
        error(['Task "' taskname '" depends on ' subjectinfo.Tasks(myDepTaskIndex).Taskname ', which hasn''t been completed yet.']);
    end
end
disp(['Running task "' taskname '"...']);
end


function CleanUp(subjectinfo, taskname)
myTaskIndex = TaskInfo.FindTask(subjectinfo, taskname);
% Set the task status
subjectinfo.Tasks(myTaskIndex) = subjectinfo.Tasks(myTaskIndex).SetComplete;
disp(['Task "' taskname '" completed.']);

% Save the subject information
subjectinfo.Save;
disp(subjectinfo);
pause(5); % Wait 5 seconds before continueing
end


%% Actual work..
function [subjectinfo] = DoWork(subjectinfo)

path = subjectinfo.Analyses.VisualResponsivenessBanded.Filepath;
xlsfilename = [path 'Results\' subjectinfo.Name ' - Feature vectors.xls'];
if exist(xlsfilename, 'file')
    delete(xlsfilename);
end

% Go through all sets
for s = subjectinfo.Analyses.VisualResponsivenessBanded.Sets
    disp (s);
    load (s.WavedataFilename);
    
    fig = plotBandedResponsivenessPerBand(data);
    saveFigure(fig, [path 'Results\'], [s.Description ' (Avg per Band)']);
    close(fig);
    
    fig = plotBandedResponsiveness(data);
    saveFigure(fig, [path 'Results\'], [s.Description ' (Banded Responsiveness)']);
    close(fig);
    
    % Tabelletjes voor Maartje (Nieuw..)
    p = zeros(length(data.bands), length(data.electrodeLabels));
    bandLabels = cell(1,9);
    for i = 1:length(data.bands)
        p(i,:) = mean(data.bands{i}.normalizedMeanDiff,1);
        bandLabels{i} = data.bands{i}.description;
    end
    pMin = min(p,[],1);
    pMax = max(p,[],1);
    
    feat1 = pMax - pMin;
    feat2 = max(abs([pMax;pMin]));
    fig = figure; 
    barh ([feat1;feat2]');
    saveFigure(fig, [path 'Results\'], [s.Description ' (Feature chart)']);
    close(fig);
  
    sheetname = [s.Identifier '-' s.Type];
    if length(sheetname)>30; sheetname = sheetname(1:30); end;
    tableContents = [{'Electrode'},{'Max'},{'Min'},{'Max-Min'},{'Max(Abs(Max,Min))'};data.electrodeLabels', num2cell(pMax'), num2cell(pMin'), num2cell(feat1'), num2cell(feat2')];
    xlswrite(xlsfilename, tableContents, sheetname, 'A1');
    
end 

end